const path = require("path");
const {
  insertCart,
  getCartList_s,
  removeCart,
  updateCart_s,
} = require("../service/cart.service");
class CartController {
  // 加入购物车
  async addCart(ctx, next) {
    // 获取商品id,商品名称,商品价格,商品数量,用户id
    const { product_id, user_id } = ctx.request.body;
    console.log("product_id", product_id);
    console.log("user_id", user_id);
    const res = await insertCart(product_id, user_id);
    if (res) {
      ctx.response.body = {
        code: "10010",
        message: "加入购物车成功",
      };
    }
    await next();
  }

  // 获取购物车列表
  async getCartList(ctx, next) {
    const { user_id } = ctx.query;
    const res = await getCartList_s(user_id);
    if (res) {
      ctx.response.body = {
        code: "10010",
        message: "获取购物车列表成功",
        data: res,
      };
    }

    await next();
  }

  // 删除购物车商品
  async deleteCart(ctx, next) {
    const { userId, productId } = ctx.request.body;
    console.log("user_id", userId);
    console.log("product_id", productId);
    const res = await removeCart({ userId, productId });
    if (res) {
      ctx.response.body = {
        code: "10010",
        message: "删除购物车商品成功",
      };
    }
    await next();
  }

  // 更改购物车状态
  async updateCart(ctx, next) {
    const { userId, productId, checked, allChecked } = ctx.request.body;
    const res = await updateCart_s({ userId, productId, checked, allChecked });
    if (res) {
      ctx.response.body = {
        code: "10010",
        message: "更改购物车状态成功",
      };
    }
    await next();
  }
}

module.exports = new CartController();
